package com.juc.semaphore.semaphore18;

import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;

public class Service {
     /**
      * 多进路 ->单处理 -> 多出路实验
      * 允许多个线程同时处理任务，但执行任务的顺序却是同步的，也就是阻塞的
      * 所以是单处理
      */
	 private Semaphore semaphore = new Semaphore(3);
	 private ReentrantLock look = new ReentrantLock();
	 
	 public void sayHello() {
		 try {
			semaphore.acquire();
			System.out.println("threadName = " + Thread.currentThread().getName() + " 准备！ ");
			look.lock();
			System.out.println("begin hello " + System.currentTimeMillis());
			for (int i = 0; i < 5; i++) {
				System.out.println(Thread.currentThread().getName() + " 打印 " + (i + 1));
			}
			System.out.println("end hello " + System.currentTimeMillis());
			look.unlock();
			semaphore.release();
			System.out.println("threadName = " + Thread.currentThread().getName() + " 结束！ ");
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	 }
}
